<?php
namespace admin\customer;
use controller;

use model\admin\customerGroupModel;
use model\admin\settingModel;

class customer_group extends controller{

    public function index(){

        $default_custoemr_group_id = settingModel::get('config','customer_group_id');
        $customer_group_list = customerGroupModel::get_list( );
        foreach( $customer_group_list as &$val ){
            $val['is_default'] = $val['id'] == $default_custoemr_group_id ? 1:0;
        }
        echo json_encode( $customer_group_list );

    }

    public function info(){

        $res = [
            'status'=>0
        ];

        if( empty($_GET['customer_group_id']) ){

            $res['status'] = 1;
            $res['description'] = '请求不正确';
        }

        $customer_group_id = $_GET['customer_group_id'];
        $customer_group = customerGroupModel::info( $customer_group_id );

        $res['customer_group'] = $customer_group;

        echo json_encode( $res );

    }

    public function edit(){

        $res = [
            'status'=>0
        ];


        if( empty( $_POST['name'] ) ){
            $res['status'] = 1;
            $res['description'] = '名称不能为空';
            echo json_encode( $res );
            die();
        }


        $data['name'] = $_POST['name'];
        $data['description'] = $_POST['description'];
        $data['sort_order'] = $_POST['sort_order'];

        if( empty($_GET['customer_group_id']) ){

            $customer_group_id = customerGroupModel::create( $data );

        }else{

            $customer_group_id = $_GET['customer_group_id'];
            $num_rows = customerGroupModel::edit( $customer_group_id, $data );
            $res['num_rows'] = $num_rows;

        }


        $res['customer_group_id'] = $customer_group_id;
        echo json_encode( $res );
    }

    public function delete(){

        $res = [
            'status'=>0
        ];
        if( empty($_POST['del_customer_groups']) ){

            $res['status'] = 1;
            $res['description'] = '请求不正确';
        }

        $del_customer_groups = $_POST['del_customer_groups'];



        $num_rows = customerGroupModel::delete( $del_customer_groups );

        $res['num_rows'] = $num_rows;

        echo json_encode( $res );
    }


}

